package com.qf.controller;


import com.qf.annotation.IsLogin;
import com.qf.entity.Coupon;
import com.qf.entity.PricesParams;
import com.qf.entity.R;
import com.qf.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * (Coupon)表控制层
 *
 * @author makejava
 * @since 2020-10-14 14:33:55
 */
@RestController
@RequestMapping("/coupon")
public class CouponController  {
    /**
     * 服务对象
     */
    @Autowired
    private CouponService couponService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/list")
    @IsLogin
    public R<List<Coupon>> couponlist(Integer method){
        List<Coupon> coupons = couponService.list(method);
        return new R<List<Coupon>>().setData(coupons);
    }

    @RequestMapping("/insert")
    public R<Boolean> couponSave(@RequestBody Coupon coupon){
        boolean falg = couponService.save(coupon);
        return new R<Boolean>().setData(falg);
    }

    /**
     * 领取优惠券
     * @return
     */
    @IsLogin(mustLogin = true)
    @RequestMapping("/get")
    public R<Boolean> couponGet(Integer cid){
        boolean flag = couponService.couponGet(cid, 0);
        return new R<Boolean>().setData(flag);
    }

    /**
     * 优惠券的使用列表 - 订单编辑页面查询
     * @return
     */
    @IsLogin(mustLogin = true)
    @RequestMapping("/userList")
    public R<List<List<Coupon>>> couponUserList(PricesParams pricesParams){
        List<List<Coupon>> coupons = couponService.userList(pricesParams);
        return new R<List<List<Coupon>>>().setData(coupons);
    }

    /**
     * 根据优惠券id查询优惠券信息
     * @param cid
     * @return
     */
    @RequestMapping("/queryById")
    public R<Coupon> queryById(@RequestParam Integer cid){
        Coupon coupon = couponService.getById(cid);
        return new R<Coupon>().setData(coupon);
    }

    /**
     * 根据开抢的时间查询优惠券列表
     * @param beginTime
     * @return
     */
    @RequestMapping("/queryByTimes")
    public R<List<Coupon>> queryByTimes(@RequestBody Date beginTime){
        List<Coupon> coupons = couponService.queryByTimes(beginTime);
        return new R<List<Coupon>>().setData(coupons);
    }

    /**
     * 抢劵提醒的功能
     * @return
     */
    @RequestMapping("/tixing")
    public R msgTixing(Integer cid, String devid){

        //获取优惠券的信息
        Coupon coupon = couponService.getById(cid);
        Date beginTime = coupon.getGoStartTime();
        //日历对象
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginTime);
        calendar.add(Calendar.MINUTE, -10);
        Date tixingTime = calendar.getTime();

        //将信息保存到redis中
        String key = "tixing_" + new SimpleDateFormat("yyMMdd").format(tixingTime);
        double score = Double.valueOf(new SimpleDateFormat("HHmm").format(tixingTime));
        redisTemplate.opsForZSet().add(key, devid + "-" + cid, score);

        return new R().setData(true);
    }
}